﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.Scenes.ProcessScene.Store;
using Microsoft.Xna.Framework.Input.Touch;
using Spinning.Spinning.Interface;

namespace Spinning.Spinning.Entity.Scenes
{
    class StoreScene : BaseScene, ISaveLoad
    {
        TextEntity totalText, scoreText;
        SubStoreScene sub;

        public override void OnLoad()
        {
            base.OnLoad();
            Sprite backGroundSprite = new Sprite();
            backGroundSprite.textureName = "bg";
            backGroundSprite.ZIndex = Constants.background;
            backGroundSprite.OnLoad();
            this.Attach(backGroundSprite);

            Sprite itemTitleSprite = new Sprite();
            itemTitleSprite.textureName = "item_store/txt_store";
            itemTitleSprite.ZIndex = Constants.ui;
            itemTitleSprite.OnLoad();
            itemTitleSprite.Position = new Vector2(170, 60);
            this.Attach(itemTitleSprite);

            Sprite barSprite = new Sprite();
            barSprite.textureName = "item_store/bar";
            barSprite.ZIndex = Constants.uibackground;
            barSprite.OnLoad();
            barSprite.Position = new Vector2(75, 40);
            this.Attach(barSprite);

            sub = new SubStoreScene();
            sub.Position = new Vector2(31, 165);
            sub.Color = Color.Transparent;
            sub.ZIndex = Constants.uibackground;
            sub.OnLoad();
            Attach(sub);

            ButtonSprite backSprite = new ButtonSprite();
            backSprite.textureName = "item_store/btn_return";
            backSprite.ZIndex = Constants.ui;
            backSprite.Position = new Vector2(390, 726);
            backSprite.OnLoad();
            TouchManager.RegisteTouchArea(backSprite);
            this.Attach(backSprite);
            backSprite.OnTouchEvent += OnBackTouch;

            ButtonSprite okSprite = new ButtonSprite();
            okSprite.textureName = "item_store/btn_ok";
            okSprite.ZIndex = Constants.ui;
            okSprite.Position = new Vector2(390, 672);
            okSprite.OnLoad();
            TouchManager.RegisteTouchArea(okSprite);
            this.Attach(okSprite);
            okSprite.OnTouchEvent += OnOkTouch;

            ResourceManager.ContentRoot = "";
            totalText = new TextEntity();
            totalText.fontString = "Century Gothic";
            totalText.OnLoad();
            totalText.Origin = new Vector2(1, 0.5f);
            totalText.Position = new Vector2(375, 702);
            totalText.Color = Color.White;
            totalText.ZIndex = Constants.debugText;
            totalText.Scale = new Vector2(1.2f);
            Attach(totalText);
            totalText.Value = "700"; //測試

            scoreText = new TextEntity();
            scoreText.fontString = "Century Gothic";
            scoreText.OnLoad();
            scoreText.Origin = new Vector2(1, 0.5f);
            scoreText.Position = new Vector2(375, 754);
            scoreText.Color = new Color(54, 56, 43);
            scoreText.ZIndex = Constants.debugText;
            scoreText.Scale = new Vector2(1.2f);
            Attach(scoreText);
            scoreText.Value = "120346";//測試
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);
            totalText.Value = sub.TotalPrice.ToString();
        }

        void OnBackTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            Game1.stageManager.GoToScene("role");
        }

        void OnOkTouch(BaseEntity entity, TouchLocation touchLocation)
        {
            //用分數結帳
            Save(Game1.stageManager.GameSrate);

        }

        public override BaseScene Clone()
        {
            StoreScene scene = new StoreScene();
            scene.Size = new Vector2(Constants.Window_Width, Constants.Window_Height);
            scene.OnLoad();
            scene.SortChildren();
            scene.Load(Game1.stageManager.GameSrate);
            return scene;
        }

        public void Save(Unitity.Storage.SaveData state)
        {         
            int score = state.TotalScore - sub.TotalPrice;
            if (score < 0) { }
            else
            {
                state.TotalScore = score;
                scoreText.Value = score.ToString();
                totalText.Value = "" + 0;
                sub.Save(state);
            }
        }

        public void Load(Unitity.Storage.SaveData state)
        {
            sub.Load(state);
            scoreText.Value = state.TotalScore.ToString();
        }
    }
}
